﻿<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>自定义属性</title>
  <style>
    #box {
      width: 200px;
      height: 200px;
      background: skyblue;
    }

  </style>
</head>

<body>
  <!-- data 开头的自定义属性
       dataset  只要你定义的属性是以data开头，都能操作
       getAttribute  获取指定标签属性的值
       setAttribute  设置指定标签的属性值
  -->
  <div id="box" class="box" data-aaa="two"></div>
</body>
<script>
  var one = document.getElementById('box'); //通过id获取到元素
  console.log(one.getAttribute('class')); //通过获取的元素找到里面class属性的元素
  console.log(one.getAttribute('data-aaa')); //获取到自定义属性的属性值
  one.setAttribute('data-aaa', 'three') // 修改自定义属性值
  console.log(one.getAttribute('data-aaa'))

  console.log(box.dataset.aaa);
  one.dataset.aaa = 'bbb';
  console.log(box.dataset.aaa);
  one.dataset.bbb = 'boxbbb';
  console.log(box.dataset.bbb);

</script>

</html>
